home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / modules / watchdog.module < prev   
Encoding:
Text File  |  2005-01-27  |  5.9 KB  |  164 lines

  1. <?php
  2. // $Id: watchdog.module,v 1.121 2005/01/26 23:02:44 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * System monitoring and logging for administrators.
  7.  *
  8.  * The watchdog module monitors your site and keeps a list of
  9.  * recorded events containing usage and performance data, errors,
  10.  * warnings, and similar operational information.
  11.  *
  12.  * @see watchdog().
  13.  */
  14.  
  15. /**
  16.  * Implementation of hook_help().
  17.  */
  18. function watchdog_help($section = 'admin/help#watchdog') {
  19.   switch ($section) {
  20.     case 'admin/logs':
  21.       return t('<p>The watchdog module monitors your web site, capturing system events in a log to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information.  It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.</p>');
  22.     case 'admin/modules#description':
  23.       return t('Logs and records system events.');
  24.   }
  25. }
  26.  
  27. /**
  28.  * Implementation of hook_menu().
  29.  */
  30. function watchdog_menu($may_cache) {
  31.   $items = array();
  32.  
  33.   if ($may_cache) {
  34.     $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
  35.       'callback' => 'watchdog_overview', 'access' => user_access('administer watchdog'));
  36.     $items[] = array('path' => 'admin/logs/event', 'title' => t('details'),
  37.       'callback' => 'watchdog_event', 'access' => user_access('administer watchdog'),
  38.       'type' => MENU_CALLBACK);
  39.   }
  40.   return $items;
  41. }
  42.  
  43. /**
  44.  * Implementation of hook_perm().
  45.  */
  46. function watchdog_perm() {
  47.   return array('administer watchdog');
  48. }
  49.  
  50. /**
  51.  * Implementation of hook_cron().
  52.  *
  53.  * Remove expired log messages and flood control events.
  54.  */
  55. function watchdog_cron() {
  56.   db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
  57.   db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
  58. }
  59.  
  60. /**
  61.  * Menu callback; displays a listing of log messages.
  62.  */
  63. function watchdog_overview() {
  64.   $icons = array(WATCHDOG_NOTICE  => '',
  65.                  WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
  66.                  WATCHDOG_ERROR   => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
  67.   $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
  68.  
  69.   $names['all'] = t('all messages');
  70.   $queries['all'] = '';
  71.   foreach (_watchdog_get_message_types() as $type) {
  72.     $names[$type] = t('%type messages', array('%type' => t($type)));
  73.     $queries[$type] = "WHERE type = '". db_escape_string($type) ."'";
  74.   }
  75.  
  76.   if (empty($_SESSION['watchdog_overview_filter'])) {
  77.     $_SESSION['watchdog_overview_filter'] = 'all';
  78.   }
  79.  
  80.   $op = $_POST['op'];
  81.   if ($op == t('Filter') && isset($_POST['edit']['filter'])) {
  82.     $_SESSION['watchdog_overview_filter'] = $_POST['edit']['filter'];
  83.   }
  84.  
  85.   $form  = form_select(t('Filter by message type'), 'filter', $_SESSION['watchdog_overview_filter'], $names);
  86.   $form .= form_submit(t('Filter'));
  87.  
  88.   $header = array(
  89.     ' ',
  90.     array('data' => t('Type'), 'field' => 'w.type'),
  91.     array('data' => t('Date'), 'field' => 'w.timestamp', 'sort' => 'desc'),
  92.     array('data' => t('Message'), 'field' => 'w.message'),
  93.     array('data' => t('User'), 'field' => 'u.name'),
  94.     array('data' => t('Operations'), 'colspan' => '2')
  95.   );
  96.   $sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid '. $queries[$_SESSION['watchdog_overview_filter']] . tablesort_sql($header);
  97.   $result = pager_query($sql, 50);
  98.  
  99.   while ($watchdog = db_fetch_object($result)) {
  100.     $rows[] = array('data' =>
  101.       array(
  102.         // Cells
  103.         $icons[$watchdog->severity],
  104.         t($watchdog->type),
  105.         format_date($watchdog->timestamp, 'small'),
  106.         truncate_utf8($watchdog->message, 64),
  107.         format_name($watchdog),
  108.         $watchdog->link,
  109.         l(t('details'), "admin/logs/event/$watchdog->wid")
  110.       ),
  111.       // Attributes for tr
  112.       'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
  113.     );
  114.   }
  115.  
  116.   if (!$rows) {
  117.     $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '7'));
  118.   }
  119.  
  120.   $pager = theme('pager', NULL, 50, 0, tablesort_pager());
  121.   if (!empty($pager)) {
  122.     $rows[] = array(array('data' => $pager, 'colspan' => '7'));
  123.   }
  124.  
  125.   $output  = '<div class="container-inline">'. form($form) .'</div>';
  126.   $output .= theme('table', $header, $rows);
  127.  
  128.   print theme('page', $output);
  129. }
  130.  
  131. /**
  132.  * Menu callback; displays details about a log message.
  133.  */
  134. function watchdog_event($id) {
  135.   $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
  136.   $output = '';
  137.   $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
  138.   if ($watchdog = db_fetch_object($result)) {
  139.     $output .= '<table border="1" cellpadding="2" cellspacing="2">';
  140.     $output .= ' <tr><th>'. t('Type') .'</th><td>' . t($watchdog->type) . '</td></tr>';
  141.     $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($watchdog->timestamp, 'large') .'</td></tr>';
  142.     $output .= ' <tr><th>'. t('User') .'</th><td>'. format_name($watchdog) .'</td></tr>';
  143.     $output .= ' <tr><th>'. t('Location') ."</th><td>". l($watchdog->location, $watchdog->location) ."</td></tr>";
  144.     $output .= ' <tr><th>'. t('Message') ."</th><td>$watchdog->message</td></tr>";
  145.     $output .= ' <tr><th>'. t('Severity') .'</th><td>'. $severity[$watchdog->severity] .'</td></tr>';
  146.     $output .= ' <tr><th>'. t('Hostname') ."</th><td>$watchdog->hostname</td></tr>";
  147.     $output .= '</table>';
  148.   }
  149.   print theme('page', $output);
  150. }
  151.  
  152. function _watchdog_get_message_types() {
  153.   $types = array();
  154.  
  155.   $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  156.   while ($object = db_fetch_object($result)) {
  157.     $types[] = $object->type;
  158.   }
  159.  
  160.   return $types;
  161. }
  162.  
  163. ?>
  164.